package com.itheima.web;

import com.alibaba.fastjson.JSON;
import com.itheima.pojo.Brand;
import com.itheima.pojo.PageBean;
import com.itheima.service.BrandService;
import com.itheima.service.impl.BrandServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

@WebServlet("/brand/*")
public class BrandServlet extends BaseServlet{

    private BrandService brandService=new BrandServiceImpl();

    public void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Brand> brands = brandService.selectAll();

        String s = JSON.toJSONString(brands);

        resp.setContentType("text/json;charset=utf-8");
        resp.getWriter().write(s);
    }

    public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        req.setCharacterEncoding("utf-8");

        BufferedReader reader = req.getReader();

        String s = reader.readLine();

        Brand brand = JSON.parseObject(s, Brand.class);

        brandService.add(brand);

        resp.getWriter().write("success");
    }

    public void del(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        req.setCharacterEncoding("utf-8");

        int id=Integer.parseInt(req.getParameter("id"));



        brandService.del(id);

        resp.getWriter().write("success");
    }

    public void delAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        req.setCharacterEncoding("utf-8");

        BufferedReader reader = req.getReader();

        String s = reader.readLine();

        int[] ids = JSON.parseObject(s, int[].class);

        for (int id : ids) {
            System.out.println(id);
        }

        brandService.delAll(ids);

        resp.getWriter().write("success");
    }

    public void selectAllPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        req.setCharacterEncoding("utf-8");

        PageBean<Brand> pageBean = brandService.selectAllPage(Integer.parseInt(req.getParameter("begin")),Integer.parseInt(req.getParameter("size")));

        String s = JSON.toJSONString(pageBean);

        resp.setContentType("text/json;charset=utf-8");
        resp.getWriter().write(s);
    }

    /**
     * @description:分页条件查询
     * @author: yanhongwei
     * @date: 2023/3/17 10:49
     * @param: [req, resp]
     * @return: void
     **/
    public void selectByPageAndCondition(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        req.setCharacterEncoding("utf-8");

        BufferedReader reader = req.getReader();

        String s1 = reader.readLine();

        Brand brand=JSON.parseObject(s1,Brand.class);

        PageBean<Brand> pageBean = brandService.selectByPageAndCondition(Integer.parseInt(req.getParameter("begin")),Integer.parseInt(req.getParameter("size")),brand);

        String s = JSON.toJSONString(pageBean);

        resp.setContentType("text/json;charset=utf-8");
        resp.getWriter().write(s);
    }



}
